//app.js
import Touches from './utils/Touches.js'
var common = require("utils/common.js");
var courierHelper = require("utils/courier.js");
const __utils = require('utils/util');
var Bmob = require('utils/Bmob-1.6.5.min.js');
Bmob.initialize("3b7bc81ed038b60c3cc63922ede49a33", "f76a0a13d6ac18cc84cd255a940011cd");

App({
  version: 'v2.2.8', //版本号
  onLaunch: function () {
    var that = this;
    //调用系统API获取设备的信息
    wx.getSystemInfo({
      success: function (res) {
        var kScreenW = res.windowWidth / 375
        var kScreenH = res.windowHeight / 603
        wx.setStorageSync('kScreenW', kScreenW)
        wx.setStorageSync('kScreenH', kScreenH)
      }
    })


    //获取经纬度
    that.getLoca();

    //调用API从本地缓存中获取数据
    try {
      var value = wx.getStorageSync('user_openid')
      if (value) {
        that.changeByOpenid(value)
      } else {

        Bmob.User.auth().then(res => {
          console.log(res)
          console.log('一键登陆成功')
          console.log(res.authData.weapp.openid)
          wx.setStorageSync('user_openid', res.authData.weapp.openid)
          that.changeByOpenid(res.authData.weapp.openid)
          // wx.setStorageSync('user_id', user.id)
          //                   wx.setStorageSync('my_nick', user.get("nickname"))
          //                   wx.setStorageSync('my_username', user.get("username"))
          //                   wx.setStorageSync('my_sex', user.get("sex"))
          //                   wx.setStorageSync('my_avatar', user.get("userPic"))

        }).catch(err => {
          console.log(err)
        });



      }
    } catch (e) {
      console.log("登陆失败")
    }


    wx.checkSession({
      success: function () { },
      fail: function () {
        //登录态过期，重新登录
        wx.login()
      }
    }), // 获取用户信息
      wx.getSetting({
        success: res => {
          if (res.authSetting['scope.userInfo']) {
            // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
            wx.getUserInfo({
              success: res => {
                // 可以将 res 发送给后台解码出 unionId
                this.globalData.userInfo = res.userInfo

                // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                // 所以此处加入 callback 以防止这种情况
                if (this.userInfoReadyCallback) {
                  this.userInfoReadyCallback(res)
                }
              }
            })
          }
        }
      })
  },
  changeByOpenid: function (openid) {

    const query = Bmob.Query('couriers');


    query.equalTo("openid", "==", wx.getStorageSync("user_openid"));
    query.equalTo("myType", "==", 1);

    
    query.count().then(res => {
        console.log(`共有${res}条记录`)
        if (res > 0) {
          console.log('是快递员');
          wx.hideTabBar({
            success: function () {
              wx.redirectTo({
                url: '/pages/courier/courier',
              })
              // wx.navigateTo({
              //   url: '/pages/courier/courier',
              // })
            }
          })
        }
        else {
            
        }

    }); 

     
  },
  onShow: function () {

  },
  formate_data: function (date) {
    let month_add = date.getMonth() + 1;
    var formate_result = date.getFullYear() + '年' +
      month_add + '月' +
      date.getDate() + '日' +
      ' ' +
      date.getHours() + '点' +
      date.getMinutes() + '分';
    return formate_result;
  },

  getLoca: function () {
    var that = this;
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        const latitude = res.latitude
        const longitude = res.longitude
        const speed = res.speed
        const accuracy = res.accuracy

        that.globalData.userLocation = {
          "latitude": latitude,
          "longitude": longitude,
          "speed": speed,
          "accuracy": accuracy
        };
      }
    })
  },

  getUserInfo: function (cb) {
    var that = this;
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      wx.login({
        success: function () {
          wx.getUserInfo({
            success: function (res) {
              that.globalData.userInfo = res.userInfo;
              typeof cb == "function" && cb(that.globalData.userInfo)
            }
          })
        }
      });
    }
  },
  globalData: {
    userInfo: null,
    baseHost: "https://localhost:808/",
    userLocation: {}
  },
  onPullDownRefresh: function () {
    //wx.stopPullDownRefresh()
  },
  onError: function (msg) { },
  Touches: new Touches(),
  util: __utils,
})